package com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.service.impl;

import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.entity.*;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.EnterpriseLossRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.repository.ReportPublicFieldRepository;
import com.css.fxfzxczh.modules.lossReportManage.lossReportEscalation.strategy.StrategyService;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import org.apache.poi.hssf.usermodel.HSSFDataFormatter;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/02/15
 * @Description: 企业地震损失报表
 */
//@Service("企业地震损失报表")
@Service("15")
public class EnterpriseLossServiceImpl implements StrategyService {
    @Resource
    EnterpriseLossRepository enterpriseLossRepository;
    @Resource
    ReportPublicFieldRepository reportPublicFieldRepository;

    @Override
    public void reportImport(Workbook workbook, String xlrmId, String previousXlrmId,ReportPublicField reportPublicField) {
        if(!PlatformObjectUtils.isEmpty(previousXlrmId)){
            enterpriseLossRepository.deleteByXlrmId(previousXlrmId);
            //删除报表公共字段
            reportPublicFieldRepository.deleteByXlrmId(previousXlrmId);
        }
        Map map = getList(workbook,xlrmId);
        List<EnterpriseLossFixedAssets> fixedAssetsList = (List<EnterpriseLossFixedAssets>) map.get("fixedAssetsList");
        List<EnterpriseLossCurrentAssets> currentAssetsList = (List<EnterpriseLossCurrentAssets>) map.get("currentAssetsList");
        EnterpriseLossComment comment = (EnterpriseLossComment) map.get("comment");
        enterpriseLossRepository.batchFixedSave(fixedAssetsList);
        enterpriseLossRepository.batchCurrentSave(currentAssetsList);
        enterpriseLossRepository.saveComment(comment);
        reportPublicField.setProvince(fixedAssetsList.get(0).getProvince());
        reportPublicField.setCity(fixedAssetsList.get(0).getCity());
        reportPublicField.setCounty(fixedAssetsList.get(0).getCounty());
        reportPublicField.setTownship(fixedAssetsList.get(0).getTownship());
        reportPublicField.setEnterpriseName(fixedAssetsList.get(0).getEnterpriseName());
        //默认删除
        reportPublicField.setDelFlag(YNEnum.Y.toString());
        reportPublicFieldRepository.save(reportPublicField);
    }

    //获取list
    private Map getList(Workbook workbook, String xlrmId){
        HashMap map = new HashMap<>();
        //企业地震损失报表_固定资产部分
        List<EnterpriseLossFixedAssets> fixedAssetsList = new ArrayList<>();
        //企业地震损失报表_流动资产部分
        List<EnterpriseLossCurrentAssets> currentAssetsList = new ArrayList<>();
        //企业地震损失报表_备注部分
        EnterpriseLossComment comment = new EnterpriseLossComment();
        if (workbook != null) {
            //循环除了第一行的所有行
            /** 得到第二个sheet */
            Sheet sheet = workbook.getSheetAt(0);
            /** 得到Excel的行数 */
            int totalRows = sheet.getPhysicalNumberOfRows();
            HSSFDataFormatter hSSFDataFormatter = new HSSFDataFormatter();
            //填表人
            String fillFormUser = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(1));
            //联系电话
            String tel = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(3));
            //填表日期
            String fillFormTime = hSSFDataFormatter.formatCellValue(sheet.getRow(1).getCell(5));
            //省
            String province = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(1));
            //市
            String city = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(3));
            //县
            String county = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(5));
            //乡
            String township = hSSFDataFormatter.formatCellValue(sheet.getRow(2).getCell(7));
            //企业名称
            String enterpriseName = hSSFDataFormatter.formatCellValue(sheet.getRow(3).getCell(1));
            //隶属关系
            String subordination = hSSFDataFormatter.formatCellValue(sheet.getRow(3).getCell(3));
            //主要产品
            String mainProduct = hSSFDataFormatter.formatCellValue(sheet.getRow(3).getCell(5));
            //年产值(万元)
            String annualProductionValue = hSSFDataFormatter.formatCellValue(sheet.getRow(4).getCell(1));
            //固定资产总值(万元)
            String fixedAssets = hSSFDataFormatter.formatCellValue(sheet.getRow(4).getCell(3));
            //流动资产总值（包括原料、产成品和在制品（万元）
            String currentAssets = hSSFDataFormatter.formatCellValue(sheet.getRow(4).getCell(5));

            comment.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            comment.setXlrmId(xlrmId);
            comment.setComment(hSSFDataFormatter.formatCellValue(sheet.getRow(totalRows).getCell(1)));

            String type=null;
            String assetType=null;

            /** 循环Excel的行 从第七行开始*/
            for (int r = 6; r < totalRows; r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
                if (hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("固定资产")){
                    type=hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                if (hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("流动资产")){
                    type=hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                if (hSSFDataFormatter.formatCellValue(row.getCell(0)).equals("备注")){
                    type=hSSFDataFormatter.formatCellValue(row.getCell(0));
                }
                //固定资产部分
                if (type.equals("固定资产")){
                    if (hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("资产分类")){
                        continue;
                    }
                    EnterpriseLossFixedAssets fixed = new EnterpriseLossFixedAssets();
                    //主键id
                    fixed.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    //报表主表id
                    fixed.setXlrmId(xlrmId);
                    //填表人
                    fixed.setFillFormUser(fillFormUser);
                    //tel
                    fixed.setTel(tel);
                    //填报日期
                    fixed.setFillFormTime(fillFormTime);
                    //省
                    fixed.setProvince(province);
                    //市
                    fixed.setCity(city);
                    //县
                    fixed.setCounty(county);
                    //乡
                    fixed.setTownship(township);
                    //企业名称
                    fixed.setEnterpriseName(enterpriseName);
                    //隶属关系
                    fixed.setSubordination(subordination);
                    //主要产品
                    fixed.setMainProduct(mainProduct);
                    //年产值(万元)
                    fixed.setAnnualProductionValue(annualProductionValue);
                    //固定资产总值(万元)
                    fixed.setFixedAssets(fixedAssets);
                    //流动资产总值（包括原料、产成品和在制品（万元）
                    fixed.setCurrentAssets(currentAssets);
                    //资产分类
                    if (!hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("")){
                        assetType=hSSFDataFormatter.formatCellValue(row.getCell(1));
                    }
                    fixed.setAssetType(assetType);
                    //资产名称
                    fixed.setAssetName(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //原值（万元）
                    fixed.setOriginalValue(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //购置/建造时间
                    fixed.setPurchaseTime(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                    //破坏状态描述
                    fixed.setDamageConditionDesc(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //破坏等级
                    fixed.setDamageGrade(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    //损失/万元
                    fixed.setLoss(hSSFDataFormatter.formatCellValue(row.getCell(7)));
                    //恢复方式
                    fixed.setRecoveryMode(hSSFDataFormatter.formatCellValue(row.getCell(8)));
                    //恢复时间/天
                    fixed.setRecoveryTime(hSSFDataFormatter.formatCellValue(row.getCell(9)));
                    //停减产损失/万元
                    fixed.setStopProductionLoss(hSSFDataFormatter.formatCellValue(row.getCell(10)));
                    //恢复投资/万元
                    fixed.setRestoreMoney(hSSFDataFormatter.formatCellValue(row.getCell(11)));
                    //序列
                    fixed.setSequence(String.valueOf(r));
                    fixedAssetsList.add(fixed);
                }
                //流动资产部分
                if (type.equals("流动资产")){
                    if (hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("资产分类")){
                        continue;
                    }
                    EnterpriseLossCurrentAssets current = new EnterpriseLossCurrentAssets();
                    current.setId(UUID.randomUUID().toString().replaceAll("-", ""));
                    current.setXlrmId(xlrmId);
                    //资产分类
                    if (!hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("")){
                        assetType=hSSFDataFormatter.formatCellValue(row.getCell(1));
                    }
                    //填表人
                    current.setFillFormUser(fillFormUser);
                    //tel
                    current.setTel(tel);
                    //填报日期
                    current.setFillFormTime(fillFormTime);
                    //省
                    current.setProvince(province);
                    //市
                    current.setCity(city);
                    //县
                    current.setCounty(county);
                    //乡
                    current.setTownship(township);
                    //企业名称
                    current.setEnterpriseName(enterpriseName);
                    //隶属关系
                    current.setSubordination(subordination);
                    //主要产品
                    current.setMainProduct(mainProduct);
                    //年产值(万元)
                    current.setAnnualProductionValue(annualProductionValue);
                    //固定资产总值(万元)
                    current.setFixedAssets(fixedAssets);
                    //流动资产总值（包括原料、产成品和在制品（万元）
                    current.setCurrentAssets(currentAssets);
                    //资产分类
                    if (!hSSFDataFormatter.formatCellValue(row.getCell(1)).equals("")){
                        assetType=hSSFDataFormatter.formatCellValue(row.getCell(1));
                    }
                    current.setAssetType(assetType);
                    //资产名称
                    current.setAssetName(hSSFDataFormatter.formatCellValue(row.getCell(2)));
                    //原值
                    current.setOriginalValue(hSSFDataFormatter.formatCellValue(row.getCell(3)));
                    //总数量
                    current.setTotal(hSSFDataFormatter.formatCellValue(row.getCell(4)));
                    //破坏状态描述
                    current.setDamageConditionDesc(hSSFDataFormatter.formatCellValue(row.getCell(5)));
                    //损失/万元
                    current.setLoss(hSSFDataFormatter.formatCellValue(row.getCell(6)));
                    current.setSequence(String.valueOf(r));
                    currentAssetsList.add(current);
                }
            }
        }
        map.put("fixedAssetsList",fixedAssetsList);
        map.put("currentAssetsList",currentAssetsList);
        map.put("comment",comment);
        return map;
    }
}
